﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ozner.Common;
using Ozner.BLL;
using Ozner.Model;
using System.Threading;

namespace Ozner.UI
{
    public partial class FrmLogin : FrmBaseDev
    {
        private UserBLL userBLL = UserBLL.GetInstance();

        /// <summary>
        /// 表示是否是更改账户
        /// </summary>
        public bool IsChange { get; internal set; }

        public FrmLogin()
        {
            InitializeComponent();
        }
        #region 事件
        #region 退出按钮点击事件
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        #endregion
        #region 窗体加载事件
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtAccount.Focus();
            txtRememberMe.Checked = ConfigModel.AutoLogin;
            txtAccount.Text = ConfigModel.UserName;
            txtPassword.Text = ConfigModel.Password;
            if (!IsChange && ConfigModel.AutoLogin)
            {
                Login();
            }
            
        }
        #endregion
        #region 登录按钮点击事件
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }
        #endregion
        #region 忘记密码被点击
        private void linkForgetPassword_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(ConfigModel.BaseUrl + ConfigModel.LoginUrl);
        }
        #endregion
        #endregion
        #region 登录方法
        private void Login()
        {
            ShowWaiting("正在登录.....");
            string tip = "";
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                tip = "密码不能为空!";
                txtPassword.Focus();
            }
            if (string.IsNullOrEmpty(txtAccount.Text))
            {
                tip = "账号不能为空!";
                txtAccount.Focus();
            }
            lblError.Text = tip;
            if (string.IsNullOrEmpty(tip))
            {
                BaseResult<JsonBaseResult<User>> br = userBLL.Login(txtAccount.Text, txtPassword.Text, txtRememberMe.Checked);
                if (br.Code == Code.OK && br.Object.Success == "true")
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }else
                {
                    lblError.Text = br.Message;
                }
            }
            CloseWaiting();
        }
        #endregion
    }
}